package fu.capstone.hanoicityguide.presentation.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.Holiday;

public class ListHolidayAdapter extends ArrayAdapter<Holiday> {

	private Activity context = null;
	private int resource;
	private ArrayList<Holiday> arr = null;

	// variable for imageloader
	private DisplayImageOptions options;
	private ImageLoader imageLoader;

	public ListHolidayAdapter(Activity context, int resource,
			ArrayList<Holiday> objects) {
		super(context, resource, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.arr = objects;

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				getContext()).build();
		if (!ImageLoader.getInstance().isInited()) {
			ImageLoader.getInstance().init(config);
		}
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_photos)
				.showImageForEmptyUri(R.drawable.ic_photos)
				.showImageOnFail(R.drawable.ic_error).cacheInMemory(true)
				.cacheOnDisk(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		// set view for list view item
		LayoutInflater inflater = context.getLayoutInflater();
		convertView = inflater.inflate(resource, null);

		Holiday holiday = arr.get(position);
		ImageView holidayImage = (ImageView) convertView
				.findViewById(R.id.holidayImage);
		TextView holidayTitle = (TextView) convertView
				.findViewById(R.id.holidayTitle);
		// set place icon
		imageLoader.displayImage(arr.get(position).getIconImage(),
				holidayImage, options, null);

		holidayTitle.setText(holiday.getName());

		return convertView;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arr.size();
	}

	public void setArr(ArrayList<Holiday> arr) {
		this.arr = arr;
	}

}